package edu.chalmers.scv;

import javax.microedition.khronos.opengles.GL10;

import android.content.Context;
import edu.chalmers.scavengers.R;
import edu.chalmers.targine.Arbject;
import edu.chalmers.targine.Billboard;

//TODO: fix pending treasure so that game does not crash when there are two players
public class ScavengerArbject extends Arbject{
	private String treasure = null;
	private String pendingTreasure = null;
	private boolean trapped = false;
	
	private Billboard trapBillboard;
	
	private static int unicornTex = -1;
	private static int chestTex = -1;
	private static int cupTex = -1;
	private static int trapTex = -1;
	
	public static String EMPTY = "empty";
		
	public ScavengerArbject(String name, String patternID, String nfcID, String deviceID, Context context) {
		super(name, patternID, nfcID, context);
	}
	
	public void setTreasure(String treasure){
		if(resetModels){
			pendingTreasure = treasure;
		} else{
			this.treasure = treasure;
		}
		markForReset();
	}
	
	public boolean hasTreasure(){
		return treasure != null && !treasure.equals(EMPTY);
	}
	
	public void removeTreasure(){
		treasure = EMPTY;
		pendingTreasure = null;
		markForReset();
	}
	
	public void setTrap(){
		trapped = true;
		trapBillboard.setVisible(true);
		markForReinitialization();
	}
	
	public boolean isTrapped(){
		return trapped;
	}
	
	public int takeTreasure(String ownTreasure, String badTreasure){
		int value = 0;
		if(trapped){
			value = -2;
		} else{
			if(treasure.equals(ownTreasure)){
				value = 2;
			} else if(treasure.equals(badTreasure)){
				value = -1;
			} else{
				value = 1;
			}
		}
		removeTreasure();
		return value;
	}
	
	@Override
	public void init(GL10 gl){
		if(unicornTex == -1){
			unicornTex = loadTexture(gl, mContext, R.drawable.treasure_unicorn);
			chestTex = loadTexture(gl, mContext, R.drawable.treasure_chest);
			cupTex = loadTexture(gl, mContext, R.drawable.treasure_cup);
			trapTex = loadTexture(gl, mContext, R.drawable.trap);
		}
		
		if(trapBillboard == null){
			trapBillboard = new Billboard(trapTex, 0f, 0f, -1f, 0.5f, 90f);
			trapBillboard.setVisible(false);
		}
		
		super.init(gl);
		
		if(treasure != null){
			if(treasure.equals(Scavengers.UNICORN)){
				addBillboard(unicornTex, 0f, 0f, 0f, 60f, -90f);
			} else if(treasure.equals(Scavengers.CHEST)){
				addBillboard(chestTex, 0f, 0f, 0f, 60f, -90f);
			} else if(treasure.equals(Scavengers.CUP)){
				addBillboard(cupTex, 0f, 0f, 0f, 60f, -90f);
			}
			addBillboard(trapBillboard);
		}
	}
	
	@Override
	public void draw(GL10 gl){
		super.draw(gl);
		
		if(pendingTreasure != null){
			setTreasure(pendingTreasure);
			pendingTreasure = null;
		}
	}
}
